package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class StudentDao implements BaseStudentDao {
    //集合容器更换
    private static Student[] student = new Student[5];

    //新建新人进行方便直接测试
   /* static {
        *//*Student s1 = new Student("001", "张三", "25", "1998");
        Student s2 = new Student("002", "张三2", "225", "19298");
        student[0] = s1;
        student[1] = s2;*//*
        //通过IO读取stu.txt中的数据进内存,生成学生对象并装载进集合

    }*/

    //添加学生
    public boolean addStudent(Student stu) {
        //标记位思想
        int index = -1;
        //遍历数组
        for (int i = 0; i < student.length; i++) {
            if (student[i] == null) {
                index = i;
                break;
            }
        }
        //根据index的值明确本次添加是否成功
        if (index == -1) {
            return false;
        } else {
            student[index] = stu;
            return true;
        }
    }

    //查找学生
    public Student[] findAllStudent() {
        return student;
    }

    //删除学生
    public void deleteStudentById(String delId) {
        int index = getIndex(delId);
        if (index != -1) {
            student[index] = null;
        }
    }

    //获取学生ID
    public int getIndex(String delId) {
        int index = -1;
        for (int i = 0; i < student.length; i++) {
            if (student[i] != null && student[i].getId().equals(delId)) {
                index = i;
                break;
            }
        }
        return index;
    }

    //修改学生
    public void updateStudent(Student stu) {
        int index = getIndex(stu.getId());
        student[index] = stu;
    }
}
